//
//  TLLoadControl.m
//  FunnyVideo
//
//  Created by 李鑫 on 15/8/20.
//  Copyright © 2015年 NewEggTech. All rights reserved.
//
#define ControlSize [indicator frame].size.width
#define LoadActionTriggerHeight 20.f
#define LoadControlX [TLDeviceManger deviceScreenSize].width/2 - ControlSize/2
#define TopBarHeight CGRectGetHeight([[UIApplication sharedApplication] statusBarFrame]) + 44
#define OriginalHeight(H) UIEdgeInsetsMake(H, 0, 0, 0)
#import "AppDelegate.h"
#import "TLLoadControl.h"
#import "MONActivityIndicatorView.h"

@interface TLLoadControl ()<MONActivityIndicatorViewDelegate>
{
    MONActivityIndicatorView *indicator;
    
    TLControlState state;
    
    
}
@property (nonatomic,strong) TLControlStatusCallBack statusBlock;
@property (nonatomic,weak) UIScrollView *targetScrollView;
@property (nonatomic,readonly) UIEdgeInsets originalInset;
@end
@implementation TLLoadControl
@synthesize statusBlock,originalInset;

- (void)setOriginalInset:(UIEdgeInsets )originalInset_{
    originalInset = originalInset_;

}
- (id)initWithScrollView:(UIScrollView *)scrollView{
    
    self = [super init];
    
    if(self)
    {
       // AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
        
       // CGFloat appHeight = [appDelegate window].frame.size.height;
        self.targetScrollView = scrollView;
        
        NSLog(@"%f",scrollView.contentInset.top);
        indicator = [[MONActivityIndicatorView alloc] initWithFrame:CGRectMake(LoadControlX, scrollView.contentSize.height, ControlSize, ControlSize/2)];


        indicator.delegate = self;
        indicator.numberOfCircles = 4;
        indicator.radius = 8;
        //indicator.hidden = YES;
        indicator.internalSpacing = 8;
        indicator.duration = 0.35f;
        [indicator startAnimating];
        //reset the load control
        [indicator setFrame:CGRectMake(LoadControlX, scrollView.frame.size.height/2 - ControlSize/2, ControlSize, ControlSize/2)];
        
        //scrollView.delegate = self;
        NSLog(@"%@",scrollView.delegate);
        
        [scrollView addSubview:indicator];
        
        
        state = TLControlStateEnd;
    }
    return self;
}

#pragma mark -- Block Pass
- (void)statusCallBack:(TLControlStatusCallBack)block{
    self.statusBlock = block;
}

#pragma mark -- Event
- (void)stop{
    //Make a stop over
    if(self.statusBlock)
    {
        self.statusBlock(TLControlStateEnd);
        state = TLControlStateEnd;
    }
    
    [self makeRecovery:self.targetScrollView];
}

#pragma mark -- UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //滑倒了底部
    if((scrollView.frame.size.height + scrollView.contentOffset.y) >= scrollView.contentSize.height + LoadActionTriggerHeight)
    {
        NSLog(@"%lf ---- > %lf",scrollView.frame.size.height + scrollView.contentOffset.y,scrollView.contentSize.height + LoadActionTriggerHeight);
        //Make a stop over
        if(self.statusBlock && state == TLControlStateEnd)
        {
        self.statusBlock(TLControlStateBegin);
        state = TLControlStateLoading;
            
        [self makeStopOver:scrollView];
        }
    }
}

#pragma mark -- Animations
- (void)makeStopOver:(UIScrollView *)scrollView{
    //[UIView animateWithDuration:0.2 animations:^{
        [indicator setFrame:CGRectMake(LoadControlX, scrollView.contentSize.height + LoadActionTriggerHeight, ControlSize, ControlSize/2)];
        indicator.hidden = NO;
        [scrollView setContentInset:UIEdgeInsetsMake(0, 0, ControlSize, 0)];
    //}];
}

- (void)makeRecovery:(UIScrollView *)scrollView{
    [UIView animateWithDuration:1.0f
                          delay:0.4f
         usingSpringWithDamping:0.4f
          initialSpringVelocity:0.8f
                        options:UIViewAnimationOptionCurveLinear
                     animations:^{
                         indicator.hidden = YES;
                         [scrollView setContentInset:UIEdgeInsetsZero];
                     } completion:nil];
}

#pragma mark -
#pragma mark - MONActivityIndicatorViewDelegate Methods

- (UIColor *)activityIndicatorView:(MONActivityIndicatorView *)activityIndicatorView
      circleBackgroundColorAtIndex:(NSUInteger)index {

    if(index == 0)
    return [UIColor colorWithRed:( 232)/255.0 green:( 222)/255.0 blue:( 35)/255.0 alpha:1];
    else if (index == 1)
    return [UIColor colorWithRed:( 169)/255.0 green:( 209)/255.0 blue:( 68)/255.0 alpha:1];
    else if(index == 2)
        return [UIColor colorWithRed:( 100)/255.0 green:( 158)/255.0 blue:( 214)/255.0 alpha:1];
    else
        return AdornColor;
}


@end
